package langnstats.project.lib.clustering;

import java.util.HashMap;
import java.util.Map;

import langnstats.project.lib.WordType;

public class SuperTypeClusterer extends AbstractClusterer {

	public static enum Type{
		BASIC, COLON_PERION_CLUSTERED, PUNC_CLUSTERED;
	}
	private Type type = null;
	
	public SuperTypeClusterer(){
		this(Type.BASIC);
	}
	public SuperTypeClusterer(Type type){
		this.type = type;
	}
	
	private Map<String,Integer> idMap = new HashMap<String,Integer>();
	private int nextID = 1;
	
	private int getOrCreateID(String s){
		Integer id = idMap.get(s);
		if(id==null){
			int returnID = nextID;
			idMap.put(s, nextID++);
			return returnID;
		}
		return id;		
	}
	
	public int getClusterID(WordType wt) {
		if(type==Type.PUNC_CLUSTERED){
			if(wt.getName().length()>4){
				return getOrCreateID("PUNC");				
			}
		}else if(type==Type.COLON_PERION_CLUSTERED){
			if(wt==WordType.COLON || wt==WordType.PERIOD){
				return getOrCreateID("PER");
			}
		}
		
		String wtName = wt.getName();
		if(wtName.length()>4){ return getOrCreateID(wtName); }
		else{ return getOrCreateID(wt.getName().substring(0,2)); }
	}
	
	public double getProbability(WordType wt, double clusterProbability) {
		return clusterProbability/this.countWordTypeInSameCluster(wt);
	}
	public String getName() {
		return "SuperTypeClusterer ("+type.name()+")";
	}

}
